<script type="text/javascript">

	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		force_p_newlines : false,
		force_br_newlines : true,
	 paste_retain_style_properties : "margin, padding, width, height, font-size, font-weight, font-family, color, text-align, ul, ol, li, text-decoration, border, background, float, display,text-indent",
		
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
	paste_create_paragraphs : true,
       

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "pasteword,preview,forecolor,backcolor,image,outdent,indent,link,unlink",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		
	});
	
	
</script>
<?php
$prefix = 'news';
if($_POST['action']=='save')
{
	$data = array(
		'subject'=>$_POST['title'],
		'detail'=>$_POST['content']
	);	
	if($_POST['id']){
		update("tb_$prefix",$data,"id = '".$_POST['id']."'");
		$id = $_POST['id'];
	}else{
		insert("tb_$prefix",$data);	
		$id = mysql_insert_id();	
	}
	$fileupload=$_FILES['fileupload']['tmp_name'];
	$fileupload_name=$_FILES['fileupload']['name'];
	$fileupload_size=$_FILES['fileupload']['size'];
	$fileupload_type=$_FILES['fileupload']['type'];
	
	if ($fileupload) {
			$array_last=explode(".",$fileupload_name);
			$c=count($array_last)-1; 
			$lastname=strtolower($array_last[$c]) ;
			$photoname=date("YmdHis")."_".$id.".".$lastname;
			$image = new SimpleImage();
			$image->load($fileupload);
			
			if($image->getWidth()>100){
				$image->resizeToWidth(100);
			}
			if($image->getHeight()>100){
				$image->resizeToHeight(100);
			}
			$image->save("../upload/$photoname");
			$data = array(
				"image"=>($photoname),
			);
			update("tb_$prefix",$data,"id='".$id."'");											
			unlink($fileupload);
		}
		echo "<script>window.location='admin.php?page=$prefix'</script>";
}
$sql = "select * from tb_$prefix where id = '".$_GET['id']."'";
$qr = select($sql);
$rs = $qr[0];
?>
<div id="dialog_active" title="ข้อความ"  style="display:none;">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>ต้องการเปลี่ยนสถานะ?</p>
</div>
<div id="dialog2" title="ข้อความ"  style="display:none;">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>ต้องการลบข้อมูล?</p>
</div>
<div id="member_dialog" title="ข้อมูลสมาชิก"  style="display:none;"></div>
<div id="tabs">
    <ul>
    	<li><a class="head_tab" style="padding-left:7px;" href="#tabs-1"><span class="ui-icon ui-icon-calendar" style="float:left;"></span>News</a></li>
    </ul>
    <div id="tabs-1">
    	<form action="" method="post" enctype="multipart/form-data" id="content-form">
          <table width="100%" border="0" cellspacing="0" cellpadding="5">
                  <tr>
                    <td width="8%"><strong>Title</strong></td>
                    <td width="92%">
                    <input name="title" type="text" id="title" value="<?=$rs['subject']?>" size="50" style="width:450px;" /></td>
                </tr>
                  <tr>
                    <td valign="top"><strong>Content</strong></td>
                    <td>
                    <textarea name="content" id="content" cols="45" rows="25"><?=$rs['detail']?></textarea></td>
                </tr>
                  <tr>
                    <td><strong>Image</strong></td>
                    <td>
                    <?php
						if($rs['image']<>''){
							echo "<img src=\"../upload/$rs[image]\" width=100/>";
						}
					?>
                    <input type="file" name="fileupload" id="fileupload" />
                    ขนาดรูปภาพที่แนะนำ 139*139 pixel</td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="button" id="button" value="Submit" />
                    <input name="id" type="hidden" id="id" value="<?=$rs['id']?>" />
                    <input name="action" type="hidden" id="action" value="save" /></td>
                  </tr>
          </table>
      </form>
    </div>
</div>            